﻿using System;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using RestServicesSample.Model;
using RestServicesSample.Services;

namespace RestServicesSample
{
    public partial class FormUser : PhoneApplicationPage
    {
        private SampleApi service;
        private int currentAction;

        public class ValidateUserInput
        {
            public static bool Validate(User user)
            {
                if (String.IsNullOrEmpty(user.Name) ||
                    String.IsNullOrEmpty(user.Surname) ||
                    String.IsNullOrEmpty(user.Email) ||
                    String.IsNullOrEmpty(user.Phone))
                {
                    return false;
                }

                return true;
            }
        }

        public FormUser()
        {
            InitializeComponent();
            service = new SampleApi();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string action;
            NavigationContext.QueryString.TryGetValue("action", out action);
            currentAction = int.Parse(action);

            if (currentAction == Global.UpdateAction)
            {
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = true;

                txtName.Text = Global.Instance.User.Name;
                txtSurname.Text = Global.Instance.User.Surname;
                txtEmail.Text = Global.Instance.User.Email;
                txtPhone.Text = Global.Instance.User.Phone;
            }
            else
            {
                ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;
            }
        }

        public void Submit(User user)
        {
            if (currentAction == Global.UpdateAction)
            {
                service.UpdateUser(user,
                    success =>
                    {
                        MessageBox.Show("Success!"); 
                    },
                    fail =>
                    {
                        MessageBox.Show("Something goes wrong!"); 
                    });
            }
            else
            {
                service.InsertUser(user,
                    success =>
                    {
                        MessageBox.Show("Success!"); 
                    },
                    fail =>
                    {
                        MessageBox.Show("Something goes wrong!"); 
                    });
            }
        }

        private void Button_Click(object sender, EventArgs e)
        {
            User user = new User()
            {
                Name = txtName.Text,
                Surname = txtSurname.Text,
                Email = txtEmail.Text,
                Phone = txtPhone.Text
            };

            if (!ValidateUserInput.Validate(user))
                MessageBox.Show("Please fill all fields.");

            Submit(user);
        }

        private void DeleteContactClicked(object sender, EventArgs e)
        {
            service.DeleteUser(Global.Instance.User.Id,
                success =>
                {
                    MessageBox.Show("Contact removed with success!");

                    NavigationService.GoBack();
                },
                fail =>
                {
                    MessageBox.Show("It was not possible remove contact " + Global.Instance.User.Name);
                });
        }
    }
}